﻿using MediatR;
using SheepCultivation.Common.Domain;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.AccountingunitsW
{
    public class AccountingUnitIdSeachHandler : IRequestHandler<AccountingUnitIdSeachCommon, Result>
    {
        private readonly IBaseRepository<Accountingunit> AccountingunitRepository;
        public AccountingUnitIdSeachHandler(IBaseRepository<Accountingunit> accountingunitRepository)
        {
            AccountingunitRepository = accountingunitRepository;
        }

        public async Task<Result> Handle(AccountingUnitIdSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                if (request.AccountingunitId == -1)
                {
                    var list = AccountingunitRepository.GetAll().Where(x => x.Pid == 0).ToList();
                    res.Data = list;
                }
                else
                {
                    var list = AccountingunitRepository.GetAll().Where(x => x.Pid == request.AccountingunitId).ToList();
                    res.Data = list;
                }
            }
            catch (Exception ex)
            {

            }
            return res;
        }
    }
}
